package com.qqtech.qquan.user.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.draw.model.DrawBatchQuery;
import com.qqtech.qquan.draw.model.DrawItemVo;
import com.qqtech.qquan.draw.service.DrawBatchService;
import com.qqtech.qquan.draw.service.DrawItemService;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.model.UserDraw;
import com.qqtech.qquan.user.model.UserDrawQuery;
import com.qqtech.qquan.user.model.UserDrawVo;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.qquan.user.service.UserDrawService;
import com.qqtech.web.context.BaseWebController;
@Controller
@RequestMapping(value = "/user/draw")
public class UserDrawController extends BaseWebController<UserDraw, UserDrawQuery> {
	@Resource
	private UserDrawService userDrawService;
	
	@Resource
	private DrawItemService drawItemService;
	
	@Resource
	private UserBaseService userBaseService;
	
	
	@Resource
	private DrawBatchService drawBatchService;
    
	@Override
	protected BaseService<UserDraw> getBaseService() {
		return userDrawService;
	}
	
	/**
	 * add
	 * 
	 * @param id
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/add")
	@ResponseBody
	public Result add(@RequestBody DrawBatchQuery drawBatchQuery) {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		result=userDrawService.addUserDraw(curUserId,drawBatchQuery.getType(),drawBatchQuery.getBizzId(),drawBatchQuery.getDrawBaseId());
		UserDrawQuery userDrawQuery = new UserDrawQuery();
		userDrawQuery.setDrawBaseId(drawBatchQuery.getDrawBaseId());
		List<UserDrawVo> userDrawList = userDrawService.queryList(userDrawQuery, new PageKit(1,20,new PageOrder(Direction.DESC, "id")));
		for (UserDrawVo userDrawVo : userDrawList) {
			DrawItemVo drawItemVo=drawItemService.getById(userDrawVo.getDrawItemId());
			userDrawVo.setDrawItemName(drawItemVo.getName());
			UserBaseVo userBaseVo=userBaseService.getById(userDrawVo.getUserId());
			userDrawVo.setUserNick(userBaseVo.getNick());
		}
		result.put("userDrawList", userDrawList);
		return result;
	}
	/**
	 * 管理列表
	 * 
	 * @param UserDrawQuerys
	 * @return
	 */
	@RequestMapping(value = "/managePage")
	@ResponseBody
	public Result managePage(@RequestBody UserDrawQuery userDrawQuery) {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		userDrawQuery.setUserId(curUserId);
		Pageable pageKit = new PageKit(userDrawQuery.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<UserDrawVo> page = userDrawService.queryPageList(userDrawQuery, pageKit);
		UserBaseVo userBaseVo=null;
		DrawItemVo drawItemVo=null;
		for (UserDrawVo userDrawVo : page) {
			drawItemVo=drawItemService.getById(userDrawVo.getDrawItemId());
			userDrawVo.setDrawItemName(drawItemVo.getName());
			userBaseVo=userBaseService.getById(userDrawVo.getUserId());
			userDrawVo.setUserNick(userBaseVo.getNick());
			userDrawVo.setDrawItemImg(drawItemVo.getPathImgs());
			if(StringUtils.isNotBlank(drawItemVo.getImg())){
				userDrawVo.setHasImg(true);
			}else{
				userDrawVo.setHasImg(false);
			}
		}
		result.put("page", page);
		return result;
	}
}
